const api = require('../../../utils/api.js');
const regeneratorRuntime = require('../../../utils/runtime.js');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    keyword: "",
    nodata: false,
    nomore: false,
    detailPath: "../college/college",
    isShowModel: false,
    isShowDeleteModel: false,
    currentId: "", //当前操作的id
    where: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: async function(options) {
    let that = this;
    that.setData({
      page: 1,
      list: [],
      nomore: false,
      nodata: false
    })
    let where = {}
    await this.getList(where)
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function(e) {
    let where = this.data.where
    await this.getList(where)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: async function(e) {
    let that = this;
    let page = 1
    that.setData({
      page: page,
      list: [],
      nomore: false,
      nodata: false
    })
    await this.getList(that.data.where)
    wx.stopPullDownRefresh();
    console.log('pull down')
  },

  /**
   * 搜索功能
   * @param {} e 
   */
  bindconfirm: async function(e) {
    let that = this;
    let keyword = e.detail.value.keyword
    console.log('e.detail.value -> ', keyword)
    let page = 1
    let where = {}
    if (keyword != "") {
      where.name = keyword
      where.campus = keyword
    }
    that.setData({
      page: page,
      list: [],
      keyword: keyword,
      nomore: false,
      nodata: false,
      where: where
    })
    await this.getList(where)
  },
  /**
   * 设置隐藏
   * @param {*} e 
   */
  hideShowModal: function(e) {
    this.setData({
      isShowModel: false
    })
  },
  /**
   * 设置隐藏
   * @param {*} e 
   */
  hideLabelModal: function(e) {
    this.setData({
      isShowLabelModel: false
    })
  },
  /**
   * 设置隐藏
   * @param {*} e 
   */
  hideShowDeleteModal: function(e) {
    this.setData({
      isShowDeleteModel: false
    })
  },
 

  showDeleteModal: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    console.log('id ->', id)
    that.setData({
      isShowDeleteModel: true,
      currentId: id
    })
  },


  deleteShowModal: async function(e) {

    wx.showLoading({
      title: '加载中...',
    })
    try {
      let that = this;
      let res = await api.deleteDocById('mini_colleges', that.data.currentId)
      if (res) {
        that.setData({
          isShowDeleteModel: false,
          currentId: "",
        })

        await that.onPullDownRefresh()

        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1500
        })
      } else {
        wx.showToast({
          title: '操作发生未知异常',
          duration: 1500
        })
      }
    } catch (err) {
      wx.showToast({
        title: '程序有一点点小异常，操作失败啦',
        icon: 'none',
        duration: 1500
      })
      console.error(err)
    } finally {
      wx.hideLoading()
    }

  },
  /**
   * 获取列表
   */
  getList: async function(where) {
    wx.showLoading({
      title: '加载中...',
    })
    let that = this
    let page = that.data.page
    if (that.data.nomore) {
      wx.hideLoading()
      return
    }
    let res = await api.getCollegeList(page, where)
    console.log('list ->', res)
    if (res.data.length === 0) {
      that.setData({
        nomore: true
      })
      if (page === 1) {
        that.setData({
          nodata: true
        })
      }
    } else {
      that.setData({
        page: page + 1,
        list: that.data.list.concat(res.data),
      })
    }
    wx.hideLoading()
  },

  /**
   * 点击明细
   */
  bindDetail: function(e) {
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '../college/college?id=' + id
    })
  },
})